package com.seanzx.resolver.impl;

import com.seanzx.common.Result;
import com.seanzx.constant.SymbolTable;
import com.seanzx.lexical.Token;
import com.seanzx.resolver.IResolver;
import com.seanzx.dp.ProcessorController;
import com.seanzx.dp.entity.DropTableEntity;

import java.util.List;

public class DropTableResolver implements IResolver<DropTableEntity> {
    ProcessorController processorController = ProcessorController.get();

    @Override
    public boolean matches(List<Token> tokens) {
        return tokens.size() >= 3 &&
                tokens.get(0).getValue().equals(SymbolTable.DROP) &&
                tokens.get(1).getValue().equals("TABLE");
    }

    @Override
    public DropTableEntity translate(List<Token> tokens) {
        DropTableEntity entity = new DropTableEntity();
        if (tokens.get(3).getValue().equals(".")) {
            entity.setDatabaseName(tokens.get(2).getValue());
            entity.setTableName(tokens.get(4).getValue());
        } else {
            entity.setTableName(tokens.get(2).getValue());
        }
        return entity;
    }

    @Override
    public boolean validate(Object parameter) {
        return true;
    }

    @Override
    public Result access(Object parameter) {
        return processorController.dropTable((DropTableEntity) parameter);
    }
}
